package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApBehaviorEntryMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ApBehaviorEntryServiceImpl extends ServiceImpl<ApBehaviorEntryMapper, ApBehaviorEntry> implements ApBehaviorEntryService {


    /**
     *
     * @param userId 当前登录的用户id
     * @param equipmentId  当前登录的设备id
     * @return
     */
    @Override
    public ApBehaviorEntry findByUserIdOrEquipmentId(Integer userId, Integer equipmentId) {
        //虽然设备登录的话不能做行为 但是也要根据设备id查出来行为实体 但实际上肯定没有
        if (userId!=null){
            //查询出来用户的行为实体 返回
            return getOne(Wrappers.<ApBehaviorEntry>lambdaQuery().eq(ApBehaviorEntry::getEntryId,userId)
            .eq(ApBehaviorEntry::getType,(short)1));
        }

        if (userId==null&&equipmentId!=null&&equipmentId!=0){
            //查询终端设备登录的用户 返回
            return getOne(Wrappers.<ApBehaviorEntry>lambdaQuery().eq(ApBehaviorEntry::getEntryId,equipmentId)
                    .eq(ApBehaviorEntry::getType,(short)0));
        }

        return null;
    }
}
